package com.bxx.ermall.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bxx.ermall.common.dto.ShopCarDto;
import com.bxx.ermall.common.lang.Result;
import com.bxx.ermall.entity.ShopCar;
import com.bxx.ermall.service.ShopCarService;
import com.bxx.ermall.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("/shopcar")
public class ShopCarController {

    @Autowired
    ShopCarService shopCarService;

    @GetMapping("/list")
    public Result List(){
        List<ShopCarDto> data = shopCarService.getList();
        return Result.succ(data);
    }


    @PostMapping("/add")
    public Result add(@Validated @RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer goodsId = (Integer)jsonObject.get("goodsId");
        ShopCar shopCar = new ShopCar();
        shopCar.setGoodsId(goodsId);
        shopCar.setUserId(ShiroUtil.getProfile().getId());
        shopCarService.save(shopCar);
        return Result.succ(null);

    }

    @PostMapping("/delete")
    public Result delete(@Validated @RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        shopCarService.removeById(id);
        return Result.succ(null);
    }
}
